package com.swss.g.sys.dao.impl;

import java.sql.SQLException;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.swss.g.sys.dao.DataBaseDAO;

public class DateBaseDAOImpl extends HibernateDaoSupport implements DataBaseDAO{

	public boolean backupDB(final String url, final String fileName) throws Exception {
		// TODO Auto-generated method stub
		Boolean flag = (Boolean) this.getHibernateTemplate().execute(
				new HibernateCallback() {

					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						try {
							session.createSQLQuery(
									"BACKUP DATABASE swss2013 TO DISK='" + url
											+ fileName + "'").executeUpdate();
							return true;
						} catch (Exception e) {
							e.printStackTrace();
							return false;
						}
					}

				});
		return flag;
	}

	public boolean restoreDB(final String url, final String fileName) throws Exception {
		// TODO Auto-generated method stub
		Boolean flag = (Boolean) this.getHibernateTemplate().execute(
				new HibernateCallback() {

					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						try {
							// TODO Auto-generated method stub
							session.createSQLQuery(
									"RESTORE DATABASE swss2013 FROM DISK='" + url
											+ fileName + "'").executeUpdate();
							return true;
						} catch (Exception e) {
							e.printStackTrace();
							return false;
						}
					}
				});
		return flag;
	}

}
